#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    timer = new QTimer;
    connect(timer,SIGNAL(timeout()), this, SLOT(read_files()));
    timer->start(1000);

    serial = new QSerialPort;
    serial->setPortName("/dev/ttyS3");

    if(serial->open(QIODevice::ReadWrite))
    {
        qDebug()<<"open uart ok!";
        serial->setBaudRate(QSerialPort::Baud115200);
        serial->setDataBits(QSerialPort::Data8);
        serial->setParity(QSerialPort::NoParity);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        connect(serial,SIGNAL(readyRead()),this,SLOT(read_data()));
    }
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::read_files()
{
    QFile file("/sys/bus/i2c/drivers/bmp085/0-0077/pressure0_input");
    file.open(QIODevice::ReadOnly);
    press = file.readAll();
    file.close();

    QFile file1("/sys/bus/i2c/drivers/bmp085/0-0077/temp0_input");
    file1.open(QIODevice::ReadOnly);
    temp = file1.readAll();
    file1.close();

    QFile file2("/sys/bus/i2c/drivers/bmp085/0-0077/pressure0_input");
    file2.open(QIODevice::ReadOnly);
    hum = file2.readAll();
    file2.close();

    send_data();
}

void MainWindow::send_data(void)
{

         QString str ="<sensor_data press=\"@@@\" temp=\"###\" hum=\"$$$\"   />";

        str.replace("@@@", press);
        str.replace("###", temp);
        str.replace("$$$", hum);

        qDebug()<< str;

        serial->write(str.toLatin1());
        serial->flush();

}


void MainWindow::read_data()
{

   }
